﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace OnlineMealOrdering.Common
{
    public class EnumHelper
    {
        public static string GetEnumDescription(Enum value)
        {
            Type enumType = value.GetType();            
            string name = Enum.GetName(enumType, value);
            if (name != null)
            {
                FieldInfo fieldInfo = enumType.GetField(name);
                if (fieldInfo != null)
                {
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
                        typeof(DescriptionAttribute), false) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return null;
        }

        //public static dynamic ToSelectList<TEnum>(this TEnum enumValue)
        //{
        //    if (!typeof(TEnum).IsEnum)
        //        throw new ArgumentException("必须是枚举类型","EnumValue not in Enum!");
        //    var fields = typeof(TEnum).GetFields(BindingFlags.Public|BindingFlags.Static | BindingFlags.Public);
        //    foreach (var item in fields)
        //    {
        //        if(!item.IsDefined(typeof(DescriptionAttribute),true))
        //            throw new ArgumentException("未找到DescriptionAttribute", "enumValue"); 
        //    }

        //    var values = from e in fields
        //                 select new SelectListItem
        //                 {
        //                     Value = Convert.ToInt32(e.GetRawConstantValue()).ToString(),
        //                     Text = (e.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] as EnumDisplayNameAttribute).DisplayName,
        //                     Selected = Convert.ToInt32(e.GetRawConstantValue()) == Convert.ToInt32(enumValue)
        //                 };
        //    return values.ToList();  
        //}

        static Dictionary<Type, Dictionary<int, string>> cacheEnum = new Dictionary<Type, Dictionary<int, string>>();
        public static Dictionary<int, string> GetEmumAllValue(Type enumType)
        {
            if (cacheEnum.ContainsKey(enumType))
                return cacheEnum[enumType];
            Dictionary<int, string> dicEmumValues = new Dictionary<int, string>();

            var fidlds=enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (var fi in fidlds)
            {
                var val = fi.GetValue(null);
                var des = Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute), false) as DescriptionAttribute;
                dicEmumValues.Add((int)val,des.Description);

            }           

            cacheEnum.Add(enumType, dicEmumValues);
            return dicEmumValues;
        }

    }
}
